const mongoose = require("mongoose");
const request = require("request");
var config = require('../../../core/server/config/config.js');
var Merchant = mongoose.model("merchant");
var async = require('async');
var Dish = mongoose.model("dish");

exports.findAllMerchants = function (req, res) {
    var dishes = {};
    var merchants = {};
    async.waterfall([
        function (cb) {
            request.get({url: config.adminWS.baseUrl + config.adminWS.recommended}, function (err, response, body) {
                if(err){
                    console.error('request recommended dishes error : ' + err);
                    cb(err);
                } else {
                    var dids = JSON.parse(body);
                    //var dids = ['57bd67c4ea01f4acae0a199a', '57bd5a10cb1f488cea43fe5a', '57bd58aaa23725d0f2c7f574', '57bd581aa23725d0f2c7f573', '57bd57a6a23725d0f2c7f572'];
                    Dish.find().where('_id').in(dids).where('status').nin([0]).populate("merchant")
                        .exec(function(err,dishes_){
                            if(!err){
                                dishes = dishes_;
                                cb();
                            } else {
                                console.error('find dishes error : ' + err);
                                cb(err);
                            }
                        });
                }
            });
        },
        function (cb) {
            request.get({url: config.adminWS.baseUrl + config.adminWS.blackList}, function (err, response, body) {
                if(err){
                    console.error('request black list error : ' + err);
                    cb(err);
                } else {
                    var mids = JSON.parse(body);
                    //var mids = ['57bd81134a11269b78a1c771', '57bd81b24a11269b78a1c772'];
                    Merchant.find().where('_id').nin(mids)
                        .exec(function(err,merchants_){
                            if(!err){
                                //var dishes = {dish:dishes,mList:merchants});
                                merchants = merchants_;
                                cb();
                            } else {
                                console.error('find black list error : ' + err);
                                cb(err)
                            }
                        })
                }
            })
        }
    ], function (err) {
        if(err){
            res.json({
                error : err
            })
        } else {
            res.json({
                msg : 'SUCCESS',
                merchants : merchants,
                dishes : dishes
            })
        }
    });
};

exports.findDishByMid = function (req, res) {
    Merchant.findOne({_id: req.params.ID}).populate({
            path: 'dish',
            match: { $or : [{status: 2}, {status: 1 }]}
    }).exec(function (err, merchant) {
            if (!err) {
                res.json({
                    msg : 'SUCCESS',
                    merchant: merchant
                });
            } else {
                console.error('find merchant and dishes error : ' + err);
            }
        })
    //Merchant.findOne({_id: req.params.ID})
    //    .exec(function (err, merchant) {
    //        if (!err) {
    //
    //            async.each(merchant.dish, function (did) {
    //                Dish.findOne({_id : did}, function (err, dish) {
    //                    did = dish;
    //                })
    //            }, function (err) {
    //                res.json({
    //                    msg : 'SUCCESS',
    //                    merchant: merchant
    //                });
    //            });
    //        } else {
    //            console.error('find merchant and dishes error : ' + err);
    //        }
    //    })
};